// 前台公有js文件

// 获取热门推荐数据
$.ajax({
    type: 'get',
    url: '/posts/recommend',
    success: res => {
        // 渲染热门推荐列表
        let hotTpl = `
        {{each hots}}
        <li>
            <a href="detail.html?id={{$value._id}}">
                <img src="{{$value.thumbnail}}" alt="">
                <span>{{$value.title}}</span>
            </a>
        </li>
        {{/each}}
        `;
        let html = template.render(hotTpl, { hots: res });
        $('#hotBox').html(html);
    },
    error: err => console.log('获取热门推荐数据失败！')
});

// 获取随机推荐数据
$.ajax({
    type: 'get',
    url: '/posts/random',
    success: res => {
        // 渲染随机推荐数据
        let randomTpl = `
        {{each randoms}}
        <li>
            <a href="detail.html?id={{$value._id}}">
              <p class="title">{{$value.title}}</p>
              <p class="reading">阅读({{$value.meta.views}})</p>
              <div class="pic">
                <img src="{{$value.thumbnail}}" alt="">
              </div>
            </a>
        </li>
        {{/each}}
        `;
        let html = template.render(randomTpl, { randoms: res });
        $('#random').html(html);
    },
    error: err => console.log('获取随机推荐数据失败！')
});


// 获取最新评论数据
$.ajax({
    type: 'get',
    url: '/comments/lasted',
    success: res => {
        // 渲染最新评论列表
        let newCommentTpl = `
        {{each comments}}
        <li>
            <a href="javascript:;">
              <div class="avatar">
                <img src="{{$value.author.avatar}}" alt="">
              </div>
              <div class="txt">
                <p>
                  <span>{{$value.author.nickName}}</span>{{$imports.dateFormat($value.createAt)}}说:
                </p>
                <p>{{$value.content}}</p>
              </div>
            </a>
          </li>
          {{/each}}
        `;
        let html = template.render(newCommentTpl, { comments: res });
        $('.discuz').html(html);
    },
    error: err => console.log('获取最新评论数据失败！')
});

// 获取分类列表数据
$.ajax({
    type: 'get',
    url: '/categories',
    success: res => {
        // 渲染分类列表
        let categoryTpl = `
        {{each category}}
        <li><a href="list.html?id={{$value._id}}"><i class="fa {{$value.className}}"></i>{{$value.title}}</a></li>
        {{/each}}
        `;
        let html = template.render(categoryTpl, { category: res });
        $('.category').html(html);
    },
    error: err => console.log('获取分类列表失败！')
});

// 文章点赞
$('body').on('click', '.like', function () {
    // 获取点赞文章的id
    let id = $(this).attr('data-id');
    // 发送文章点赞请求
    $.ajax({
        type: 'post',
        url: `/posts/fabulous/${id}`,
        success: res => {
            $(this).find('span').html(`赞(${res.meta.likes})`);
        },
        error: err => console.log('文章点赞失败！')
    });
});

// 搜索表单提交事件
$('.searchForm').on('submit', function () {
    let key = $(this).find('.keys').val().trim();
    if (key.length == 0) alert('请输入搜索内容！'); 
    else location.href = `search.html?key=${key}`;

    // 阻止表单提交
    return false;
});

/**
 * 把时间转换成 2017-12-13 16:00:00  格式字符串
 * @param {要转换的时间} date 
 */
function dateFormat(date) {
    date = new Date(date);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    var second = date.getSeconds();
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
};

/**
 * 获取url参数,若url中没有要查找的参数则返回-1
 * @param {要获取的url参数名称} paramName 
 * return 获取到的参数值
 */
function getUrlParam(paramName) {
    let parmaArray = location.search.substring(1).split('&');
    for (let i = 0; i < parmaArray.length; i++) {
        let temp = parmaArray[i].split('=');
        if (temp[0] == paramName) return temp[1];
    }
    return -1;
}